﻿
<!DOCTYPE html>
<html lang="en">
<head>
    <title id='Description'>In this sample is illustrated how to create custom editors for jqxDataTable. The "First Name" and "Last Name" columns use the jqxInput widget as a cell editor. The "Products" column
        use a jqxDropDownList widget with enabled Checkboxes  as an editor. The "Quantity" column's editor is jqxSlider.
    </title>
	<meta name="description" content="JavaScript dataTable Custom Editors example" />   	    		
    <link rel="stylesheet" href="../../jqwidgets/styles/jqx.base.css" type="text/css" />
    <script type="text/javascript" src="../../scripts/jquery-1.11.1.min.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcore.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdata.js"></script> 
    <script type="text/javascript" src="../../jqwidgets/jqxbuttons.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxscrollbar.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdatatable.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxlistbox.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxdropdownlist.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxcheckbox.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxinput.js"></script>
    <script type="text/javascript" src="../../jqwidgets/jqxslider.js"></script>
    <script type="text/javascript" src="../../scripts/demos.js"></script>
    <script type="text/javascript" src="../sampledata/generatedata.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            // prepare the data
            var data = generatedata(200);

            var source =
            {
                localData: data,
                datatype: "array",
                updateRow: function (rowId, rowData, commit) {
                    // synchronize with the server - send update command
                    // call commit with parameter true if the synchronization with the server is successful 
                    // and with parameter false if the synchronization failder.
                    commit(true);
                },
                dataFields:
                [
                    { name: 'firstname', type: 'string' },
                    { name: 'lastname', type: 'string' },
                    { name: 'productname', type: 'string' },
                    { name: 'available', type: 'bool' },
                    { name: 'quantity', type: 'number' },
                    { name: 'price', type: 'number' },
                    { name: 'date', type: 'date' }
                ]
            };

            var dataAdapter = new $.jqx.dataAdapter(source);

            var getEditorDataAdapter = function (datafield) {
                var source =
                 {
                       localData: data,
                       dataType: "array",
                       dataFields:
                       [
                           { name: 'firstname', type: 'string' },
                           { name: 'lastname', type: 'string' },
                           { name: 'productname', type: 'string' },
                           { name: 'available', type: 'bool' },
                           { name: 'quantity', type: 'number' },
                           { name: 'price', type: 'number' },
                           { name: 'date', type: 'date' }
                       ]
                 };
                var dataAdapter = new $.jqx.dataAdapter(source, { uniqueDataFields: [datafield] });
                return dataAdapter;
            }

            // initialize jqxDataTable
            $("#dataTable").jqxDataTable(
            {
                width: 850,
                source: dataAdapter,
                pageable: true,
                pagerButtonsCount: 10,
                editable: true,
                autoRowHeight: false,
                columns: [
                  {
                      text: 'First Name', columntype: 'template', datafield: 'firstname', width: 180, createEditor: function (row, cellvalue, editor, cellText, width, height) {
                          // construct the editor.
                          var inputElement = $("<input style='padding-left: 4px; border: none;'/>").appendTo(editor);
                          inputElement.jqxInput({ source: getEditorDataAdapter('firstname'), displayMember: "firstname", width: width, height: height });
                      },
                      initEditor: function (row, cellvalue, editor, celltext, width, height) {
                          // set the editor's current value. The callback is called each time the editor is displayed.
                          var inputField = editor.find('input');
                          inputField.val(cellvalue);                          
                      },
                      getEditorValue: function (row, cellvalue, editor) {
                          // return the editor's value.
                          return editor.find('input').val();
                      }
                  },
                  {
                      text: 'Last Name', datafield: 'lastname', columntype: 'template', width: 180, createEditor: function (row, cellvalue, editor, cellText, width, height) {
                          // construct the editor.
                          var inputElement = $("<input  style='padding-left: 4px; border: none;'/>").prependTo(editor);
                          inputElement.jqxInput({ source: getEditorDataAdapter('lastname'), displayMember: "lastname", width: width, height: height });
                      },
                      initEditor: function (row, cellvalue, editor, celltext, width, height) {
                          var inputField = editor.find('input');
                          inputField.val(cellvalue);
                      },
                      getEditorValue: function (row, cellvalue, editor) {
                          // return the editor's value.
                          return editor.find('input').val();
                      }
                  },
                  {
                      text: 'Products', columntype: 'template', datafield: 'productname',
                      createEditor: function (row, cellvalue, editor, cellText, width, height) {
                          // construct the editor. 
                          editor.jqxDropDownList({
                              source: getEditorDataAdapter('productname'), displayMember: 'productname', valueMember: 'productname', width: width, height: height
                          });
                      },
                      initEditor: function (row, cellvalue, editor, celltext, width, height) {
                          // set the editor's current value. The callback is called each time the editor is displayed.
                          editor.jqxDropDownList({ width: width, height: height });
                          editor.val(cellvalue);
                      },
                      getEditorValue: function (row, cellvalue, editor) {
                          // return the editor's value.
                          return editor.val();
                      }
                  },
                 {
                     text: 'Quantity', width: 200, columntype: 'custom', datafield: 'quantity',
                     createEditor: function (row, cellvalue, editor, cellText, width, height) {
                         // construct the editor. 
                         editor.jqxSlider({ step: 1, mode: 'fixed', tooltip: true, showTicks: false, min: 0, max: 30, width: width, height: height });
                     },
                     initEditor: function (row, cellvalue, editor, celltext, width, height) {
                         // set the editor's current value. The callback is called each time the editor is displayed.
                         var value = parseInt(cellvalue);
                         if (isNaN(value)) value = 0;
                         editor.jqxSlider('setValue', value);
                     },
                     getEditorValue: function (row, cellvalue, editor) {
                         // return the editor's value.
                           return editor.val();
                     }
                 }
                ]
            });
        });
    </script>
</head>
<body class='default'>
    <div id="dataTable"></div>
</body>
</html>
